Okay.
So today we're going to have a look at remote events.
Now remote events are extremely cool okay.
And I think it's one of the things which you don't understand how useful and important they are.
Until you know what they are, then you kind of think, wow, you know, this is a lot more opens up,
a lot more things you can do.
Okay.
And remote events solves a bit of an issue.
Okay.
So let's say you wanted to make an invisibility potion okay.
So you have.
You can use the dot enabled, but the dot enabled event for that one.
Um, but when you interact with that tool, it makes them invisible for all other players.
Okay.
We have a bit of an issue with that because we code tools on the client, because each player has their
own tool and players are going to be interacting with a tool at different points.
So it's based on the player.
So of course we're going to be using a local script for that.
However, if we try to make the player invisible on on a local script, it will only do it locally.
And what that means is that the player will be invisible, but only on their screen.
And for everybody else the player will still be very much visible, so it won't work.
Um, which is where remote events come in.
So remote events allow you to code, um, the start coding on a local script, and then when you need
to, um, transfer information so that you can do so, so that you can keep on coding but on the server.
So it allows basically communication between the server and the client.
Okay.
This is done basically by firing an event, a remote event, um, from one script and basically picking
that up in the other.
Okay.
And you can pass over any kind of information while you're doing that as well.
So this is absolutely essential for a lot of different concepts and applications, like the, um, just
like the invisibility potion I said there.
Um, and lots of things really use input service.
If you're wanting to do last lesson, anything that you really want to do of use input service, you
probably are going to want to be using a remote event for.
Because typically when you do stuff on a, on the client, um, you usually want to pass information
over back onto the server because most things you code are on the server.
So for those topics like GUIs and and use input service that is done on local scripts, you usually
do want to use these to bring your code back over to the server.
Okay.
So let's have a look how to use these.
So I'm going to add two scripts into my test game here I'm going to add a local script inside of um
starter player scripts.
And I'm going to add a server script inside of server script service.
Okay.
These are my two scripts here.
Um okay.
So first off a remote event is actually an object.
Okay.
Um, that you fire, the object goes inside of replicator storage.
So click the plus by Replicate Storage.
And we want to add in a remote event just shown there.
Okay.
Um cool.
Now let's go over to our script.
Okay.
Let's do it.
Um, let's do it from the local script startup actually.
So let's do it from here.
Okay.
So we will need two things in order for us to start, we need to first get replicated to storage and
we need to get the remote event.
Now good practice for this is to make a variable for replicate storage first.
Then make a variable for the remote event.
In case you have multiple remote events you want to reference.
Okay so let's say local replicated.
Storage.
Now this gets a bit funny.
You could write gained replicate storage.
However, the most efficient way to write it is by getting as a service as once you get.
Unless you're referencing the workspace, it is better practice to get it using its service name.
So gain colon, get service, replicate storage like that there.
All right.
And then we can just reference that remote events.
So local remote events.
It equal to reference replicate storage.
Now again, we could write remote events like this here.
But the issue is because remote events take slightly longer to load than other objects, just like how
characters do take a bit longer to load than the players replicate the replicate replicated storage
remote events take a bit longer to load in that similar kind of way.
So we need to put a wait for child just for wait for it to appear for those extra few milliseconds that
it takes.
All right.
So let's put in here remote event.
Okay, cool.
Um, so from here, once you've got this, you want to write whatever code you want, obviously.
And once you get to the point where you want to move over from a script to a local script or a local
script to a script, um, there are free methods to do this.
Um, the they all start by referencing the remote event.
You reference it like that there, and you can either, um, fire client like that there.
You can, um, fire server or you can, um, fire all clients events.
Colon fire all.
Clients like that there.
So there are three methods to do this.
And this basically depends on what you want to do.
So this one here will send a data from a um hang on remote event colon fire.
Yes.
So fire client will send data from a server script.
So from a script to a local or in scripting terms for then information from the client to the server.
Okay.
This was an information from a server to all clients.
So this can be one specific client.
So one specific player.
But this one will do it to all players.
And then the final one will pass information over from the client to the server.
So obviously which whichever one of these you want to use depends on what script you're in.
All right.
Um, but the firewall client is also very useful as well, if you're wanting to appear for everybody,
individual ones though, you just use fire client.
All right.
Um, for this we are going to use fire server as we're in a local script, we're on the client.
We want to get server so we can just say fire server like that.
There.
Now I want this to be, um, a bit more useful than just, um, after a few seconds, fire a server.
So I'm going to add in, let's add in a GUI screen guy and add in a text button like this.
And we're going to text when this text button is selected.
Um, now this should probably then go in screen UI so it's easier to reference.
And we're just going to say um script dot parent dot text button dot mouse button, one click connect
function.
I'm not going to bother referencing as a variable because we're literally going to use it once and that's
it.
So now we're going to fire the server when this text button is clicked.
All right.
Let's then go over to the script.
We want to copy these two variables over.
As we wrote.
We need both.
We need to be remote event while we're doing this.
And so now we're fired.
The remote event.
We've started this signaling we want to then pick it up when it gets when it's fired and pick it up
again will depend on what script you're in.
You can either do it by saying remote event dot on client events, which will occur in a local script,
or on server events when it is occurs in a server script or a normal script.
So in this case, we're wanting to do it on an on server event like this here okay.
Do your standard connect function player stuff here.
And there you go.
Okay.
So um, one important bit of information is the parameters here.
So um, when you are parsing information over from the client to the server by default, it will also
pass the player.
Okay.
So if you are interested in putting data in, make sure that the first entry is the player because that's
the first part information it will pass over.
Um, we're not looking at passing information over just yet though.
Um, and then from here we can do whatever we'd like on the server.
So let's say in this case, when this occurs I want to add in a part.
So local part is equal to instance dot new parts.
Let's then say parts dot anchored is equal to true.
And part dot position is equal to vector three dot.
Let's just use what that's what.
Let's use what we learn a few lessons ago dot one times ten like that there.
All right.
And let's quickly um got our button there.
Let's quickly play the game and see what happens okay.
So we're loading in.
Let's click the button.
And that's other parts in.
Doesn't.
Okay.
That's always great to.
It's great to see, but it's not working.
Um.
Nothing's okay.
Let's see what's happened here.
Um.
Hmm.
Right.
Let's do a bit of debugging.
So print a.
Let's just see where it gets up to.
I know what it is.
Actually, I've just realized.
Didn't give it a parent.
That's probably.
Well, it's got to be the reason why.
Um, let's try this again.
Yep.
There we go.
And is the part exists.
Yep.
It's just over there nicely.
And what would happen if we did this on the client would be that I'd be able to see this part, but
nobody else would be able to see this part.
And if I was to jump on top of it, if everyone else would look like I'm just floating in mid-air.
So things like that you don't really want to be doing on the client.
Okay.
Um, but yeah, now the other way to do it would be to saying a non client event, which is the exact
same code.
It's just a slight variation on the name based on what scripture in.
Okay.
Um, okay.
On to parsing over the information.
So often you can't just continue code from another script.
Sometimes you will need to pass over certain information such as, um, I don't know which player started
it or um, a certain value that occurs when this happens.
And to do that it's very easy.
You just need to put some information inside here when you fire the server.
So for example, I could I could write a um, string here that says, um, I don't know, text button.
For example, like that there.
Okay.
Let's go a bit over to the script.
And remember we need to keep the first one as player.
The first parameter is player because that's what starts.
That's what fired over by default.
But then the second will be our second parameter which has the value of text button like this here.
So we can just call this object perhaps.
And we wanted to we could just print object just to show that it's passing over the information there.
So if we try this out.
Text box and it prints like that there.
Okay.
So it passes information over from one side to the other side.
So, um, it's really useful.
And honestly, it has lots of applications.
Um, when you are coding, we will have a look at some of the applications of this, but it allows you
to make those changes between the server and the client.
Okay.
Um, so have a play around of this and try out some things that you might not have been able to have
code coded before, for example, that, um, perhaps an invisibility potion I talked about or some
things to have use input service especially as well because you looked at last lesson because, um,
remote events really do open up, um, a lot more options for you.
Okay.
Um, so have a play around with that.
Next lesson, we're going to have a look at remote functions, which are similar to remote events,
a bit more advanced though, um, and have a bit more to them.
So, um, take a look at this and join me in the next, next lecture.
We're going to have a look at remote functions.